﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Notes;
using System.Data;

public partial class Notes_NTS_NotesList : System.Web.UI.Page
{
    NotesDB _objNotesDB = new NotesDB();
    RegistrationData _objRegistrationData = new RegistrationData();
    int DocID=0, SubjectID = 0,StreamID=0, UnivID=0;Int16? DocType=0;
    string ViewType = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {

        Label lblEmpty = NTSList.FindControl("lblEmptyMessage") as Label;
        NTSList.RequestDataSet += new Notes_UserControls_NTS_Notes_List.ReBindDataSet(UC_RequestDataSet);
        if (!IsPostBack)
        {

            if (!object.Equals(Request.QueryString["StreamID"], null))
                if (Convert.ToString(Request.QueryString["StreamID"]) != string.Empty)
                    StreamID = Convert.ToInt32(Request.QueryString["StreamID"]);
            if (!object.Equals(Request.QueryString["SubjectID"], null))
                if (Convert.ToString(Request.QueryString["SubjectID"]) != string.Empty)
                    SubjectID = Convert.ToInt32(Request.QueryString["SubjectID"]);

            if (!object.Equals(Request.QueryString["DocType"], null))
                if (Convert.ToString(Request.QueryString["DocType"]) != string.Empty)
                    DocType = Convert.ToInt16(Request.QueryString["DocType"]);

            if (!object.Equals(Request.QueryString["UnivID"], null))
                if (Convert.ToString(Request.QueryString["UnivID"]) != string.Empty)
                    UnivID = Convert.ToInt32(Request.QueryString["UnivID"]);
            if (!object.Equals(Request.QueryString["ViewType"], null))
            {
                if (Convert.ToString(Request.QueryString["ViewType"]) != string.Empty)
                {
                    ViewType = Convert.ToString(Request.QueryString["ViewType"]);
                    if (ViewType == "MyDocs")
                    {
                        if (!object.Equals(Session["User"], null))
                        {
                            _objRegistrationData = Session["User"] as RegistrationData;
                            NTSList.dsDocs = BindDocsList(0, _objRegistrationData.UserID, ViewType, StreamID, SubjectID, UnivID,DocType);
                            lblSubCatName.Text = "My Docs";
                            lblEmpty.Text = "You have not posted any document. To post a document <a href='/Notes/NTS-AddNotes.aspx'>Click Here</a>";

                        }
                        else
                            Response.Redirect(Common.LogOutPage);
                    }
                }
            }
            if (ViewType == string.Empty)
            {
                if (StreamID != 0 || SubjectID != 0 || UnivID != 0 || DocType > 0)
                {
                    NTSList.dsDocs = BindDocsList(0, 0, string.Empty, StreamID, SubjectID, UnivID,DocType);
                    lblSubCatName.Text = "Filtered Docs";
                    //NTSList.dsDocs = BindDocsList(SubjectID, 0, "DocsDetails");
                }
                else
                {
                    lblSubCatName.Text = "All Docs";
                    NTSList.dsDocs = BindDocsList(SubjectID);
                }
            }
        }

    }

    protected DataSet BindDocsList(int DocumentID, int UID = 0, string VType = "", int streamID = 0, int subjectID = 0, int univID = 0, Int16? DocType = 0)
    {
        DataSet dsDocs = new DataSet();
        dsDocs = _objNotesDB.Select_DocsMaster(DocumentID, VType, UID, streamID, subjectID, univID,false, DocType);
        if (dsDocs != null)
        {
            if (dsDocs.Tables.Count > 0)
            {
                if (dsDocs.Tables[0].Rows.Count > 0)
                    if (ViewType == "SubCat")
                        lblSubCatName.Text = Convert.ToString(dsDocs.Tables[0].Rows[0]["SubCategory"]);
            }
        }
        return dsDocs;

    }

    DataSet UC_RequestDataSet(object sender)
    {
        if (!object.Equals(Request.QueryString["ViewType"], null))
        {
            if (Convert.ToString(Request.QueryString["ViewType"]) != string.Empty)
            {
                ViewType = Convert.ToString(Request.QueryString["ViewType"]);
                if (ViewType == "MyDocs")
                {
                    if (!object.Equals(Session["User"], null))
                    {
                        _objRegistrationData = Session["User"] as RegistrationData;
                        return BindDocsList(SubjectID, _objRegistrationData.UserID, ViewType);

                    }
                }
            }
        }
        else if (!object.Equals(Request.QueryString["SubCatID"], null))
        {
            if (Convert.ToString(Request.QueryString["SubCatID"]) != string.Empty)
            {
                SubjectID = Convert.ToInt32(Request.QueryString["SubCatID"]);
                return BindDocsList(SubjectID, 0, "SubCat");
            }
        }
        else
        {
            lblSubCatName.Text = "All Docs";
            return BindDocsList(SubjectID);
        }
        return null;
    }
     
}